﻿using System;
using System.Collections.Generic;
using System.Web;
using HySoft.Cash;
using System.Net.Mail;
using HiLand.Utility.Data;

namespace PayRoller.Code
{
    public delegate void SendMailHandler(string receiverMail, string subject, string body, bool isBodyHtml, IList<Attachment> attachments);

    public class MailHelper
    {
        public static void SendMail(string receiverMail, string subject, string body)
        {
            SendMail(receiverMail, subject, body, false, null);
        }

        public static void SendMail(string receiverMail, string subject, string body,bool isBodyHtml)
        {
            SendMail(receiverMail, subject, body, isBodyHtml, null);
        }
        
        public static void SendMail(string receiverMail, string subject, string body,bool isBodyHtml, IList<Attachment> attachments)
        {
            try
            {
                if (string.IsNullOrEmpty(receiverMail) == false)
                {
                    string sentMailAccount = AppSettings.SentMailAccount;
                    MailMessage mailMessage = new MailMessage(sentMailAccount, receiverMail);
                    mailMessage.Subject = subject;
                    mailMessage.IsBodyHtml = isBodyHtml;
                    if (isBodyHtml == true)
                    {
                        body = SafeString.FormatHtml(body);
                    }
                    mailMessage.Body = body;

                    if (attachments != null)
                    {
                        for (int i = 0; i < attachments.Count; i++)
                        {
                            mailMessage.Attachments.Add(attachments[i]);
                        }
                    }

                    SmtpClient smtp = new SmtpClient();
                    smtp.EnableSsl = AppSettings.SmtpEnableSsl;

                    smtp.Send(mailMessage);
                }
            }
            catch
            { }
        }
    }
}